LoginSignup
itokyoshi70
@itokyoshi70

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

Twitter API v1.1 エンドポイントのアクセス権限不足によるエラーについて

Pythonを勉強中の大学生です。現在、Twitter APIを使用して特定のリストからツイートを自動取得し、Discordに通知するプログラムを開発しています。しかし、APIエンドポイントにアクセスしようとすると、以下のエラーが発生します。

発生している問題・エラー

403 Forbidden
453 - You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level. You can learn more here: https://developer.twitter.com/en/portal/product

このエラーを解決するための具体的な方法について教えていただけますか?

環境
Python 3.8
Tweepy 4.0
requests 2.25.1

該当するソースコード

import tweepy
import requests
from time import sleep

# Twitter APIの設定
CONSUMER_KEY = 'your_consumer_key'
CONSUMER_SECRET = 'your_consumer_secret'
ACCESS_TOKEN = 'your_access_token'
ACCESS_TOKEN_SECRET = 'your_access_token_secret'
BEARER_TOKEN = 'your_bearer_token'

# Discord WebhookのURL
WEBHOOK_URL = 'https://discord.com/api/webhooks/your_webhook_id/your_webhook_token'

# Tweepyクライアントの初期化
client = tweepy.Client(bearer_token=BEARER_TOKEN, consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET, access_token=ACCESS_TOKEN, access_token_secret=ACCESS_TOKEN_SECRET)

# リストID
list_id = 'your_list_id'  # 数値形式のリストID

while True:
    try:
        # Twitterリストからツイートを取得
        response = client.get_list_tweets(id=list_id, max_results=10)
        tweets = response.data

        if tweets:
            for tweet in reversed(tweets):  # 取得したツイートを古い順に処理
                payload = {
                    "content": tweet.text
                }
                headers = {
                    "Content-Type": "application/json"
                }
                requests.post(WEBHOOK_URL, json=payload, headers=headers)
                print(tweet.text)
                last_seen_id = tweet.id

    except tweepy.TweepyException as e:
        print(f"Error: {e}")
    
    sleep(60)  # 1分待機してから再度チェック

試したこと

・Twitter Developer PortalでAPIキー、APIシークレットキー、アクセストークン、アクセストークンシークレット、ベアラートークンを再生成しました。
・プロジェクトとアプリケーションの設定を確認し、「Read and Write」の権限を付与しました。
・アプリ設定で「Read and Write」、「Web App, Automated App or Bot」にチェックを入れています。
・Elevated Accessを申請しようとしましたが、申請ページにアクセスできませんでした。

質問

1,現在使用しているAPIがv1.1なのかv2なのかを確認する方法を教えてください。
2,Twitter API v1.1の特定エンドポイントにアクセスするための権限を取得する方法を教えてください。
3,エラー「403 Forbidden」を解決するための具体的な手順を教えてください。
4,Elevated Accessの申請ページにアクセスできない場合の対処方法を教えてください。
どなたかご存知の方がいらっしゃいましたら、ご教示いただけると助かります。

0

1Answer

参考にしてる情報がとても古い。
「プログラミング関連は情報が古くなる」ってことを覚えるのが大事。

Twitter APIが初心者の練習用にちょうど良かったのは数年前の話。
今はもうTwitter APIなんて誰も使ってないので忘れたほうがいい。

1

Your answer might help someone💌